package org.little.test.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.little.test.model.SysUser;
import org.little.test.service.SysUserService;
import org.little.test.util.MD5Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * Created by lenovo on 2017/5/8.
 */
@Controller
public class LoginCtrl {

    private static final Logger logger = LoggerFactory.getLogger(LoginCtrl.class);

    @Autowired
    private SysUserService sysUserService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String login(HttpServletRequest request,HttpSession session) {
        return "login";
    }

    @RequestMapping(value = "/loginsubmit", method = RequestMethod.POST)
    public String loginsubmit(@RequestParam(value = "loginname", required = true) String loginName,
                              @RequestParam(value = "password", required = true) String password,
                              HttpSession httpSession,
                              RedirectAttributes redirectAttributes) {
        try{
            if(StringUtils.isNotBlank(loginName) && StringUtils.isNotBlank(password)){
                //boolean loginStatus = userService.ldapAuthenticate(loginName, password);
                SysUser sysUser = sysUserService.getEffectiveUserByLoginName(loginName);
                boolean loginStatus = false;
                if(sysUser != null && StringUtils.isNotEmpty(sysUser.getPazzword())
                        && StringUtils.isNotEmpty(password)){
                    String inputPasswordMd5 = MD5Util.Md5(password);
                    if(StringUtils.equals(sysUser.getPazzword(), inputPasswordMd5)){
                        loginStatus = true;
                    }
                }
                if(loginStatus){
                        httpSession.setAttribute("TEST_USERINFO", sysUser.getUserName());
                    return "redirect:/list";
                }else{
                    //验证失败
                    redirectAttributes.addFlashAttribute("message", "用户名密码错误");
                    return "redirect:/login";
                }
            }else{
                //参数为空
                redirectAttributes.addFlashAttribute("message", "用户名密码不能为空");
                return "redirect:/login";
            }
        }catch(Exception e){
            e.printStackTrace();
            logger.error("登录出错，loginName:" + loginName + "", e);
        }
        //参数为空
        redirectAttributes.addFlashAttribute("message", "未知错误");
        return "redirect:/login";
    }

}
